package com.suitsoft.ut.web.liti;

import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.suitsoft.ut.commons.Constants;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.commons.UserHolder;
import com.suitsoft.ut.entity.base.College;
import com.suitsoft.ut.entity.base.Teacher;
import com.suitsoft.ut.entity.base.Team;
import com.suitsoft.ut.entity.thesis.Thesis;
import com.suitsoft.ut.entity.thesis.ThesisFile;
import com.suitsoft.ut.service.ServiceException;
import com.suitsoft.ut.service.thesis.ThesisFileManager;
import com.suitsoft.ut.web.CrudActionSupport;
import com.suitsoft.ut.web.InitListActionSupport;

/**
 * 立题
 *@author Suineg
 */
@SuppressWarnings("serial")
@Results({@Result(name = CrudActionSupport.RELOAD,location="liti.action",type="redirect")})
public class LitiAction  extends InitListActionSupport{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	/** 查看 */
	private final String VIEW = "view";
	/** 评阅和审核  */
	private final String PINGSHEN = "pingshen";
	
	
	/** 流程 管理 服务层对象 */
	private ThesisFileManager fileManager;
	
	
	/** 题的分页 */
	private Page<Map<String,Object>> page = new Page<Map<String,Object>>(Constants.PAGE_SIZE);
	/** 登陆教师 */
	/** 学院ID */
	private Long collegeId;
	/** 教师 */
	private String teacher;
	/** 团队 */
	private String group;
	/** 状态 */
	private Long stauts;
	/** 标识  1 - 评阅,2 - 团队,3 - 专家，4 - 基层 (专家没有评语。所以，专家审核时不会用到这个.)*/
	private Long  flag;
	
	@Override
	public String execute() throws Exception {
		Teacher teacherObj = teacherManger.findTeacherByUser(UserHolder.getLoginUser().getId());
		if(teacherObj == null){
			addActionMessage("请使用教师账号登陆！");
			return SUCCESS;
		}
		boolean f = false;
		for(Team t : teacherObj.getTeamList()){
			if(Constants.TEAM_TYPE_LITI.equals(Integer.valueOf(t.getType()))){
				f = true;
			}
		}
		if(!f){
			addActionMessage("您未在立题团队中。不能进行此操作");
			return SUCCESS;
		}
		page = thesisManager.find(page, teacher, group, Constants.THSIS_STAUTS_LITI_MANAGER,Constants.THESIS_FLAG_LITI);
		return SUCCESS;
	}
	/**
	 * 初始化立题
	 */
	public String input(){
		initList();
		if(id == null){
			thesis = new Thesis();
		}else{
			thesis = thesisManager.get(id);
		}
		return INPUT;
	}
	
	@LogAnnotation(log = "立题管理 - 保存")
	public String save(){
		try {
			if(collegeId == null || collegeId == 0l){
				addActionMessage("请选择学院！");
				return input();
			}
			College college = collegeManager.get(collegeId);
			thesis.setCollege(college);
			thesis.setTeacherNo(teacherManger.findTeacherByUser(UserHolder.getLoginUser().getId()).getTeacherNo());
			thesisManager.save(thesis);
			addActionMessage("保存成功！");
		} catch (Exception e) {
			addActionMessage("保存失败");
			addActionMessage(e.getMessage());
			e.printStackTrace();
		}
		return RELOAD;
	}
	
	@LogAnnotation(log = "立题管理 - 删除")
	public String del(){
		try {
			if(id == null || id == 0l){
				throw new ServiceException();
			}
			
			thesis = thesisManager.get(id);
			if(thesis == null){
				throw new ServiceException();
			}
			thesisFile = thesis.getThesisFile();
			thesisManager.delete(thesis.getId());
			fileManager.delete(thesisFile.getId());
			addActionMessage("删除成功！");
		} catch (ServiceException e) {
			addActionMessage("非法操作！");
			return RELOAD;
		}
		return RELOAD;
	}

	/**
	 * 查看
	 * @return
	 */
	public String view(){
		if(id == null || id == 0l){
			addActionMessage("非法操作！");
			return RELOAD;
		}
		initList();
		thesis = thesisManager.get(id);
		thesisFile = thesis.getThesisFile();
		
		return VIEW;
	}
	/**
	 * 评阅和审核
	 * @return
	 */
	public String pingshen(){
		if(id == null || id == 0l ){
			addActionMessage("非法操作！");
			return RELOAD;
		}
		initList();
		flag = 1L;
		thesis = thesisManager.get(id);
		thesisFile = thesis.getThesisFile();
		
		return PINGSHEN;
	}
	
	@LogAnnotation(log = "立题管理  - 保存评审")
	public String savePingshen(){
		if(thesisFile == null || thesisFile.getId() == 0l || flag == null || flag == 0l){
			addActionMessage("非法操作！");
			return RELOAD;
		}
		try {
			thesisManager.savePingShen(thesis, thesisFile, flag);
			addActionMessage("操作成功！");
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("保存失败！");
			return RELOAD;
		}
		return RELOAD;
	}
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Thesis getThesis() {
		return thesis;
	}

	public void setThesis(Thesis thesis) {
		this.thesis = thesis;
	}

	public ThesisFile getThesisFile() {
		return thesisFile;
	}

	public void setThesisFile(ThesisFile thesisFile) {
		this.thesisFile = thesisFile;
	}

	public Long getCollegeId() {
		return collegeId;
	}
	public void setCollegeId(Long collegeId) {
		this.collegeId = collegeId;
	}
	@Autowired
	public void setFileManager(ThesisFileManager fileManager) {
		this.fileManager = fileManager;
	}
	public Page<Map<String, Object>> getPage() {
		return page;
	}
	public void setPage(Page<Map<String, Object>> page) {
		this.page = page;
	}
	public String getTeacher() {
		return teacher;
	}
	public void setTeacher(String teacher) {
		this.teacher = teacher;
	}
	public String getGroup() {
		return group;
	}
	public void setGroup(String group) {
		this.group = group;
	}
	public Long getStauts() {
		return stauts;
	}
	public void setStauts(Long stauts) {
		this.stauts = stauts;
	}
	public Long getFlag() {
		return flag;
	}
	public void setFlag(Long flag) {
		this.flag = flag;
	}
}

